<?php

/**
 * 說明 : 使用 amazon-s3-php-class 上傳檔案到 S3
 * 
 * 設定 : 
 *
 *
 *      在config 設定 以下資料
 *              define('S3_BUCKET', '');
 *              define('S3_KEY', '');
 *              define('S3_SECRET', '');
 * 使用方法 : 
 *
 *      上傳一個檔案到s3
 *          use FS\Library\CloudStorage;
 *          $aws_s3 = new CloudStorage();
 *          $aws_s3->uploadPublicFile('/tmp/123.jpg', 's3_folder/new123.jpg');
 *          $aws_s3 = null;
 *
 *      copy s3檔案到另一個s3位置
 *          use FS\Library\CloudStorage;
 *          $aws_s3 = new CloudStorage();
 *          $aws_s3->copyFile('from/123.jpg', 'to/new123.jpg');
 *          $aws_s3 = null;
 *
 *      刪除 s3檔案
 *          use FS\Library\CloudStorage;
 *          $aws_s3 = new CloudStorage();
 *          $aws_s3->deletePublicFile('s3_folder/123.jpg');
 *          $aws_s3 = null;
 */

namespace FS\Library;


class CloudStorage
{
    // 初始化
    // ------------------------------------------------------------
    function __construct($s3_key = S3_KEY, $s3_secret = S3_SECRET) {

        \S3::setAuth($s3_key, $s3_secret);
    }

    // 上傳檔案
    // -----------------------------------
    private function uploadFile($file_path, $s3_file_path ,$acl, $s3_bucket) {

        // acl的值可以是  private,public-read...等等

        try {
            // 檢查檔案是否存在
            if(!file_exists($file_path) || !is_file($file_path)) {
                throw new Exception("file is not exist.");
             }

            //開啟 S3Exception
            \S3::setExceptions();

            // 將檔案上傳到 S3
            // 設定檔案 expire time 為 2 年
            \S3::putObject(
                \S3::inputFile($file_path, false),
                $s3_bucket,
                $s3_file_path,
                $acl,
                array(),
                array('Expires' => date("D, d M Y H:i:s T", strtotime('+2 years')))
                );
        }
        catch (\Exception $e) {
            return array('inf' => 'error', 'msg' => $e->getMessage());
        }
        catch (\S3Exception $e) {
            return array('inf' => 'error', 'msg' => $e->getMessage());
        }
        return array('inf' => 'ok');

    }

    // 刪除 s3檔案
    // -------------------------------------------------------------------------------------------------
    private function deleteFile($file_path, $bucket) {

        try {
            //開啟 S3Exception
            \S3::setExceptions();

            \S3::deleteObject($bucket, $file_path);
        }
        catch (\Exception $e) {
            return array('inf' => 'error', 'msg' => $e->getMessage());
        }
        catch (\S3Exception $e) {
            return array('inf' => 'error', 'msg' => $e->getMessage());
        }
        return array('inf' => 'ok');
    }


    // 上傳一個public檔案到s3
    // -------------------------------------------------------------------------------------------------
    public function uploadPublicFile($file_path, $s3_file_path) {

        return $this->uploadFile($file_path, $s3_file_path , S3_ACL, S3_BUCKET);
    }

    // 上傳一個private檔案到s3
    // -------------------------------------------------------------------------------------------------
    public function uploadPrivateFile($file_path, $s3_file_path) {

        return $this->uploadFile($file_path, $s3_file_path , S3_ACL_PRIVATE, S3_BUCKET_PRIVATE);
    }

    // copy s3檔案到另一個s3位置
    // -------------------------------------------------------------------------------------------------
    public function copyFile($file_path, $to_file_path, $acl = S3_ACL, $src_bucket = S3_BUCKET, $to_bucket_name = S3_BUCKET) {

        try {
            //開啟 S3Exception
            \S3::setExceptions();

            \S3::copyObject($src_bucket, $file_path, $to_bucket_name, $to_file_path, $acl);
        }
        catch (\Exception $e) {
            return array('inf' => 'error', 'msg' => $e->getMessage());
        }
        catch (\S3Exception $e) {
            return array('inf' => 'error', 'msg' => $e->getMessage());
        }
        return array('inf' => 'ok');
    }

    // 取得一個private檔案
    // -------------------------------------------------------------------------------------------------
    public function getPrivateFile($s3_file_path) {

        return \S3::getObject(S3_BUCKET_PRIVATE, $s3_file_path);

    }

    // 刪除public s3檔案
    // -------------------------------------------------------------------------------------------------
    public function deletePublicFile($file_path) {

        return $this->deleteFile($file_path, S3_BUCKET);
    }

    // 刪除private s3檔案
    // -------------------------------------------------------------------------------------------------
    public function deletePrivateFile($file_path) {

        return $this->deleteFile($file_path, S3_BUCKET_PRIVATE);
    }

}

